package com.astrob.activitys;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.astrob.adapters.MyRoutePathItemAdapter;
import com.astrob.model.HistoryData;
import com.astrob.model.Msg;
import com.astrob.model.RoutePlanDataHandle;
import com.astrob.model.RoutePlanIndex;
import com.astrob.nhcmap.R;

import java.util.ArrayList;

public class MyRoutePathActivity extends Activity implements
		MyRoutePathItemAdapter.PoiItemListener {

	private ListView mListView;
	private MyRoutePathItemAdapter mListAdapter;
	private boolean mIsLoadPath = false;
	private int mEditPathIndex;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			mIsLoadPath = extras.getBoolean("isLoadPath", false);
		}

		setContentView(R.layout.activity_myroutepath);
		TextView title = (TextView) findViewById(R.id.title);
		title.setText(R.string.myjourney_menu_item1_text);
		mListView = (ListView) findViewById(R.id.listview);
		mListView.setVerticalScrollBarEnabled(false);

		RoutePlanDataHandle.getInstance().load(this);
		mListAdapter = new MyRoutePathItemAdapter(this);
		mListAdapter.setListener(this);
		mListAdapter.setIsLoadPath(mIsLoadPath);
		mListView.setAdapter(mListAdapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
									long arg3) {
				onLoadRoute(arg2);
			}
		});

		if (mIsLoadPath) {
			ImageButton cleatBtn = (ImageButton) findViewById(R.id.clear_btn);
			cleatBtn.setVisibility(View.GONE);
			title.setText(R.string.myjourney_load_text);
		}
		updateList();
	}

	@Override
	protected void onDestroy() {
		RoutePlanDataHandle.getInstance().close();
		super.onDestroy();
	}

	private void updateList() {

		ArrayList<RoutePlanIndex> indexs = RoutePlanDataHandle.getInstance()
				.getRoutePlanIndexList();

		mListAdapter.setData(indexs);
		mListAdapter.notifyDataSetChanged();
	}

	protected void onLoadRoute(int pos) {
		RoutePlanIndex data = mListAdapter.getData(pos);

		ArrayList<HistoryData> dests = RoutePlanDataHandle.getInstance()
				.loadRoutePlan(data);
		if (dests == null) {
			return;
		}
		int count = dests.size();
		if (count <= 0) {
			return;
		}
		RoutePlanDataHandle.getInstance().setStart(dests.get(0));
		RoutePlanDataHandle.getInstance().clearDests();
		for (int i = 1; i < count; ++i) {
			RoutePlanDataHandle.getInstance().addDest(dests.get(i));
		}
		setResult(Msg.ROUTE_LOAD);
		MyRoutePathActivity.this.finish();

	}
	
	public void onBack(View v) {
		finish();
	}

	@Override
	public void onEditinfo(int pos) {
		mEditPathIndex = pos;
		RoutePlanIndex data = mListAdapter.getData(pos);
		String strEdit = data.getName();
		
		Intent intent = new Intent(MyRoutePathActivity.this,
				EditImeActivity.class);
		intent.putExtra("editstring", strEdit);
		intent.putExtra("title",
				getString(R.string.edit_myroutename_title_text));
		startActivityForResult(intent, 0);
	}

	private int mSelectIndex = -1;

	@Override
	public void onDelete(int pos) {
		mSelectIndex = pos;
		final RoutePlanIndex data = mListAdapter.getData(pos);

		new AlertDialog.Builder(this)
				.setTitle(getString(R.string.myjourney_menu_item1_text))
				.setMessage(getString(R.string.myroutepath_isclearone_text))
				.setPositiveButton(getString(R.string.yes),
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								RoutePlanDataHandle.getInstance()
										.deleteRoutePlan(data);
								updateList();
							}
						}).setNegativeButton(getString(R.string.cancel), null)
				.show();
	}

	public void onClear(View v) {
		new AlertDialog.Builder(this)
				.setTitle(getString(R.string.dlg_title))
				.setMessage(getString(R.string.myroutepath_isclearall_text))
				.setPositiveButton(getString(R.string.yes),
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								RoutePlanDataHandle.getInstance()
										.removeAllRoutePlan();
								updateList();
							}
						}).setNegativeButton(getString(R.string.cancel), null)
				.show();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == Msg.EDITED_CONTENT) {
			String strEdit = EditImeActivity.getEditResult();
			final RoutePlanIndex a = mListAdapter.getData(mEditPathIndex);
			RoutePlanDataHandle.getInstance().editRoutePlanName(a, strEdit);
			updateList();
			return;
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	protected void onResume() {
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
	}
}
